#cmake file for auxiliary tools
#author:guyadong
#created:2018/05/30

# 查找jpegwrapper库,需要设置 CMAKE_PREIX_PATH 指定 jpegwrapper,turbojpeg,openjpeg的安装路径
# 需要指定CMAKE_MODULE_PATH 指定 FindTurboJPEG.cmake 的位置
# 本例中的位置在项目根目录下/cmake/Modules
find_package(jpegwrapper REQUIRED CONFIG)

set(AUX_LIB_NAME gfaux_tools)
aux_source_directory(${CMAKE_CURRENT_LIST_DIR} _AUX_SOURCES)
file(GLOB _AUX_HEADERS RELATIVE ${CMAKE_CURRENT_LIST_DIR} "${CMAKE_CURRENT_LIST_DIR}/*.h")
set(_public_headers)
list(APPEND _public_headers ${_AUX_HEADERS})
# 用于外部调用的公共头文件
list (APPEND _public_headers
    "${DEPENDENT_SOURCE_DIR}/image_matrix_types.h"
    )
list(APPEND _AUX_SOURCES "${DEPENDENT_SOURCE_DIR}/image_matrix_types.cpp")
set(_aux_targets)
add_library(${AUX_LIB_NAME} SHARED ${_AUX_SOURCES} )
add_library(${AUX_LIB_NAME}-static STATIC ${_AUX_SOURCES} )

list(APPEND _aux_targets ${AUX_LIB_NAME} ${AUX_LIB_NAME}-static)

foreach(_target ${_aux_targets})
with_mt_if_msvc(${_target})
# MSVC Debug 编译时生成包含用于调试器的完整符号调试信息的 .obj 文件
# see also https://msdn.microsoft.com/zh-cn/library/958x11bc.aspx
target_compile_options(${_target} PRIVATE $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Debug>>:/Z7> $<$<CXX_COMPILER_ID:MSVC>:/wd4251 /wd4275>)
target_compile_definitions(${_target} 
	        PUBLIC _SL_USE_BYTE_STREAM
					)
get_target_property(_type ${_target} TYPE)
target_include_directories (${_target}
	INTERFACE "$<INSTALL_INTERFACE:include>"
					  "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>"
					  "$<BUILD_INTERFACE:${DEPENDENT_SOURCE_DIR}>"
	)
set_target_properties (${_target}  PROPERTIES 
	PUBLIC_HEADER "${_public_headers}"
	VERSION ${PROJECT_VERSION} 
	SOVERSION ${PROJECT_VERSION_MAJOR}
	FOLDER "auxiliary"
	)
set_target_properties (${_target}  PROPERTIES 
	DEBUG_POSTFIX _d
	)

set(_link_libraries faceapiclient gdface::jpegwrapper-static $<$<PLATFORM_ID:Linux>:-lX11> $<$<CXX_COMPILER_ID:GNU>:-lpthread>)

if(${_type} STREQUAL "SHARED_LIBRARY")
	target_link_libraries(${_target} PRIVATE ${_link_libraries}) 
	target_compile_definitions(${_target} 
        PUBLIC IMGMAT_USED_IN_DLL
        PRIVATE IMGMAT_EXPORTS GFAUX_EXPORTS
				)
else()
	target_link_libraries(${_target} PUBLIC ${_link_libraries})	
	target_compile_definitions(${_target} 
	        PUBLIC GFAUX_STATIC
					)
	set_target_properties (${_target}  PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON
                                                         POSITION_INDEPENDENT_CODE ON)
endif()
target_link_libraries(${_target} PRIVATE $<BUILD_INTERFACE:common_source> ) 

endforeach(_target)
################安装脚本#################
#install(TARGETS ${_aux_targets} RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} )
# ----------------------------------------------------------------------------
include (CMakePackageConfigHelpers)
set (aux_DEPENDENCY "if(USE_GFAUX_TOOLS_STATIC)
  find_dependency(jpegwrapper)
endif()
")
configure_package_config_file (${PROJECT_SOURCE_DIR}/cmake/config-aux.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config.cmake
  INSTALL_DESTINATION ${CONFIG_INSTALL_DIR}
  NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file (${AUX_LIB_NAME}-config-version.cmake VERSION
  ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion)
################安装脚本#################
install(TARGETS ${_aux_targets} EXPORT ${AUX_LIB_NAME}-targets
  RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
  LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
  ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
  PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/${AUX_LIB_NAME}
  )
install (FILES
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/${AUX_LIB_NAME}-config-version.cmake
  DESTINATION ${CONFIG_INSTALL_DIR}
  )

install (EXPORT ${AUX_LIB_NAME}-targets NAMESPACE gdface:: DESTINATION ${CONFIG_INSTALL_DIR} )

unset(AUX_LIB_NAME)
unset(_aux_targets)
unset(_public_headers)